package com.runlooker.runer.ui.personInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.runlooker.runer.R;
import com.runlooker.runer.application.MyApplication;
import com.runlooker.runer.dao.PersonInfoDao;
import com.runlooker.runer.entity.PersonInfoEntity;
import com.runlooker.runer.ui.base.BaseActivity;
import com.runlooker.runer.util.PreferenceUtils;

public class MyInfoActivity extends BaseActivity implements OnClickListener {
	private static final String tag = MyInfoActivity.class.getSimpleName();
	public final static int SUCCESS = 1;
	public final static int REQUESTCODE = 0;

	private String mAccount;
	private PersonInfoDao mPersonInfoDao;
	private PersonInfoEntity mInfo;
	private TextView mNickNameTV;
	private TextView mAccountTV;
	private TextView mSexTV;
	private TextView mAreaTV;
	private TextView mFieldTV;
	private TextView maddressTV;
	private ImageView mHeadIV;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		refreshUiByLocalData();
	}

	/**
	 * 根据本地存储的数据，刷新UI
	 */
	private void refreshUiByLocalData() {
		mInfo = mPersonInfoDao.getPersonInfo(mAccount);
		if (mInfo == null) {
			mInfo = new PersonInfoEntity();
		}
		mNickNameTV.setText(mInfo.getNickName());
		mAccountTV.setText(mInfo.getAccount());
		mSexTV.setText("男");

		mAreaTV.setText(mInfo.getCtry());
		mFieldTV.setText(mInfo.getField());
		maddressTV.setText(mInfo.getAddress());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myinfo);
		initialize();
	}

	private void initialize() {
		mAccount = PreferenceUtils.getPrefString(MyInfoActivity.this,
				MyApplication.ACCOUNT_USERNAME_KEY, "");
		mPersonInfoDao = new PersonInfoDao(this);
		mInfo = mPersonInfoDao.getPersonInfo(mAccount);

		mHeadIV = (ImageView) findViewById(R.id.myinfo_head_image);
		mNickNameTV = (TextView) findViewById(R.id.myinfo_nickname);
		mAccountTV = (TextView) findViewById(R.id.myinfo_account);
		maddressTV = (TextView) findViewById(R.id.myinfo_address);
		mSexTV = (TextView) findViewById(R.id.myinfo_sex);
		mAreaTV = (TextView) findViewById(R.id.myinfo_area);
		mFieldTV = (TextView) findViewById(R.id.myinfo_Field);

		mNickNameTV.setOnClickListener(this);
		maddressTV.setOnClickListener(this);
		mFieldTV.setOnClickListener(this);
		mAreaTV.setOnClickListener(this);

		refreshUiByLocalData();

	}

	public void back(View view) {
		finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.myinfo_nickname:
			Intent intent1 = new Intent(this, ModifyActivity.class);
			intent1.putExtra(ModifyActivity.TITLE, "诨名");
			if (mInfo == null) {
				intent1.putExtra(ModifyActivity.CONTENT, "");
			} else {
				intent1.putExtra(ModifyActivity.CONTENT, mInfo.getNickName());
			}
			intent1.putExtra(ModifyActivity.ITEM, ModifyActivity.NICK_NAME);
			startActivityForResult(intent1, REQUESTCODE);
			break;

		case R.id.myinfo_address:
			Intent intent2 = new Intent(this, ModifyActivity.class);
			intent2.putExtra(ModifyActivity.TITLE, "我的地址");
			if (mInfo == null) {
				intent2.putExtra(ModifyActivity.CONTENT, "");
			} else {
				intent2.putExtra(ModifyActivity.CONTENT, mInfo.getAddress());
			}
			intent2.putExtra(ModifyActivity.ITEM, ModifyActivity.ADDRESS);
			startActivityForResult(intent2, REQUESTCODE);
			break;

		case R.id.myinfo_area:
			Intent intent3 = new Intent(this, ModifyActivity.class);
			intent3.putExtra(ModifyActivity.TITLE, "地区");

			if (mInfo == null) {
				intent3.putExtra(ModifyActivity.CONTENT, "");
			} else {
				intent3.putExtra(ModifyActivity.CONTENT, mInfo.getCtry());
			}
			intent3.putExtra(ModifyActivity.ITEM, ModifyActivity.AREA);
			startActivityForResult(intent3, REQUESTCODE);
			break;

		case R.id.myinfo_Field:
			Intent intent4 = new Intent(this, ModifyActivity.class);
			intent4.putExtra(ModifyActivity.TITLE, "诗号");

			if (mInfo == null) {
				intent4.putExtra(ModifyActivity.CONTENT, "");
			} else {
				intent4.putExtra(ModifyActivity.CONTENT, mInfo.getField());
			}
			intent4.putExtra(ModifyActivity.ITEM, ModifyActivity.FIELD);
			startActivityForResult(intent4, REQUESTCODE);
			break;
		default:
			break;
		}
	}

}
